Copper.TaskForm = (function() {
    var submitBtn;
    
    var cancelBtn;
    
    var el;
    
    var form;
    
    var mode = 'schedule';
    
    return {
        init: function(ct){
            el = ct;
            form = el.querySelector('FORM');
            submitBtn = el.querySelector('.primary-btn');
            cancelBtn = el.querySelector('.secondary-btn');
            
            //event listener bind
            submitBtn.addEventListener('click', Copper.TaskForm.onSubmit, false);            
            cancelBtn.addEventListener('click', Copper.TaskForm.onCancel, false);
            
            var toggles = form.elements['enableTimeSetting'];
            toggles[0].addEventListener('change', Copper.TaskForm.switchMode, false);
            toggles[1].addEventListener('change', Copper.TaskForm.switchMode, false);
        },
        
        onSubmit: function(){
            var elements = form.elements;
            var taskId = elements['taskId'].value;
            var startTime = elements['startTime'].value;
            var endTime = elements['endTime'].value;
            var content = elements['content'].value;
            var alarmBefore = elements['alarmBefore'].value;
            var frequency = elements['frequency'].value;
            
            /*
            if(mode == 'todo'){ 
                Copper.ToDoList.addItem(content);
                Copper.TaskForm.hide();  
                return;
            }
            */
            
            var task = {
                id: taskId,
                startTime: startTime,
                endTime: endTime,
                content: content,
                alarmBefore: alarmBefore,
                frequency: frequency
            };
            
            if(taskId){
                Copper.TaskManager.updateTask(task);
            } else {
                Copper.TaskManager.addTask(task);
            }
            
            Copper.TaskForm.hide();
        },
        
        onCancel: function(){
            Copper.TaskForm.hide();
        },
        
        show: function(showMode){

            
            el.style.display = '-ms-flexbox';
            //el.style.display = '-webkit-box';
            Copper.Util.showScreenMask(); 
            mode = showMode;            
        },
        
        hide: function(){
            el.style.display = 'none';
            Copper.Util.hideScreenMask();
        },
        
        setValue: function(v){
            var elements = form.elements;
            elements['taskId'].value = v.taskId;
            elements['startTime'].value = v.startTime;
            elements['endTime'].value = v.endTime;
            elements['content'].value = v.content;
            elements['alarmBefore'].value = v.alarmBefore;
            elements['frequency'].value = v.frequency;
        },
        
        clearForm: function(){
            var elements = form.elements;
            elements['taskId'].value = '';
            elements['startTime'].value = '';
            elements['endTime'].value = '';
            elements['content'].value = '';
            elements['frequency'].value = 5;
        },
        
        switchMode: function(){
            var rows = el.querySelectorAll('.input-group');

            if(this.value == 'open'){
                rows[1].style.display = 'block';
                rows[2].style.display = 'block';
                rows[3].style.display = 'block';
            } else {
                rows[1].style.display = 'none';
                rows[2].style.display = 'none';
                rows[3].style.display = 'none';
            }
        }
    };
})();